//
//  CommentSwitchView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/23.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "CommentSwitchView.h"

@implementation CommentSwitchView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = CSWhiteColor;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"标题";
        titleLabel.font = MidBigFont;
        titleLabel.textColor = CSColorFromRGB(0x04030B);
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UISwitch *titleSwitch = [[UISwitch alloc] init];
        titleSwitch.onTintColor = CSColorFromRGB(0x008AFF);
        [self addSubview:titleSwitch];
        self.titleSwitch = titleSwitch;
        
        UIView *lineView= [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [self addSubview:lineView];
        self.lineView = lineView;;
        
        [titleLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.mas_equalTo(self);
        
        make.left.mas_equalTo(self).offset(10);
        
        make.width.mas_equalTo(200);
        
        make.height.mas_equalTo(60);
    }];
    
    
    [self.titleSwitch mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self).offset(-10);
        
        make.centerY.mas_equalTo(self);
        
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self);
        
        make.left.mas_equalTo(self).offset(10);
        
        make.right.mas_equalTo(self);
        
        make.height.mas_equalTo(1);
    }];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    [self strWithRed];

}


-(void)strWithRed{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.titleLabel.text];
    
    [str addAttribute:NSForegroundColorAttributeName value:CSRedColor range:[self.titleLabel.text rangeOfString:@"*"]];
    
    self.titleLabel.attributedText = str;
}

-(void)dealloc{
    
    [self.titleLabel removeObserver:self forKeyPath:@"text"];
    self.titleLabel = nil;
}
@end
